﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.DXErrorProvider;

namespace QuanLyThuNgan
{
    public partial class Frm_DM_LyDoXoa : DevExpress.XtraEditors.XtraForm
    {
        public Frm_DM_LyDoXoa()
        {
            InitializeComponent();
        }
        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        DMLyDoXoaDTO lyDoXoaDto = new DMLyDoXoaDTO();
        DMLyDoXoaCTL lyDoXoaCtl = new DMLyDoXoaCTL();

        private void Frm_DM_LyDoXoa_Load(object sender, EventArgs e)
        {
            DataTable dtld = lyDoXoaCtl.m_SelectLyDoXoa();
            grdLyDoXoa.DataSource = dtld;

            //set condition load form----------------------------
            gvLyDoXoa.OptionsBehavior.ReadOnly = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
            gvLyDoXoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)grdLyDoXoa.DataSource;
            if (dt != null)
            {
                for (int i = 0; i < gvLyDoXoa.RowCount; i++)
                {
                    DataRow dr = gvLyDoXoa.GetDataRow(i);
                    gvLyDoXoa.MoveNext();
                    gvLyDoXoa.MovePrev();
                    if (dr["TenLyDoXoaCT"].ToString() != "")
                    {
                        lyDoXoaDto.STenLyDoXoaCT = dr["TenLyDoXoaCT"].ToString().Trim();
                        lyDoXoaDto.BStop = Uit.it_Parse.ToBoolean(dr["Stop"].ToString());
                        if (dr["IDLyDoXoaCT"].ToString() == "")
                        {
                            lyDoXoaCtl.m_InsertDMLyDoXoa(lyDoXoaDto);
                        }
                        else
                        {
                            lyDoXoaDto.IIDLyDoXoaCT = Uit.it_Parse.ToInteger(dr["IDLyDoXoaCT"].ToString());
                            lyDoXoaCtl.m_UpdateDMLyDoXoa(lyDoXoaDto);
                        }
                    }

                }
                XtraMessageBox.Show("Lưu thành công");
                DataTable dtldload = lyDoXoaCtl.m_SelectLyDoXoa();
                grdLyDoXoa.DataSource = dtldload;
            }
            

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            gvLyDoXoa.OptionsBehavior.ReadOnly = true;
            gvLyDoXoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gvLyDoXoa.GetDataRow(gvLyDoXoa.FocusedRowHandle);
            if (dr != null)
            {
                lyDoXoaDto.IIDLyDoXoaCT = Uit.it_Parse.ToInteger(dr["IDLyDoXoaCT"].ToString());
                lyDoXoaCtl.m_DeleteDMLyDoXoa(lyDoXoaDto);
            }
            XtraMessageBox.Show("Xóa thành công");
            DataTable dtldload = lyDoXoaCtl.m_SelectLyDoXoa();
            grdLyDoXoa.DataSource = dtldload;
            //reset load du lieu----------------------
            gvLyDoXoa.ActiveFilterEnabled = false;
            gvLyDoXoa.ClearColumnsFilter();
            gvLyDoXoa.OptionsFilter.Reset();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            gvLyDoXoa.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = false;
            btnInsert.Enabled = false;
            gvLyDoXoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            DataTable dtldload = lyDoXoaCtl.m_SelectLyDoXoa();
            grdLyDoXoa.DataSource = dtldload;
            //reset load du lieu----------------------
            gvLyDoXoa.ActiveFilterEnabled = false;
            gvLyDoXoa.ClearColumnsFilter();
            gvLyDoXoa.OptionsFilter.Reset();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            gvLyDoXoa.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            gvLyDoXoa.OptionsBehavior.ReadOnly = true;
            DataTable dtldload = lyDoXoaCtl.m_SelectLyDoXoa();
            grdLyDoXoa.DataSource = dtldload;
            gvLyDoXoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gvLyDoXoa.ActiveFilterEnabled = false;
            gvLyDoXoa.ClearColumnsFilter();
            gvLyDoXoa.OptionsFilter.Reset();
        }

        private void gvLyDoXoa_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            GridView view = gvLyDoXoa;
            GridColumn colLyDo = view.Columns["TenLyDoXoaCT"];

            //get value in row---------------------------
            int iRowIndex = e.RowHandle;
            //if (sColRow != "bNgungSuDung")
            
            string sTenLyDo = view.GetRowCellValue(e.RowHandle, colLyDo).ToString().ToUpper().Trim();
            if (sTenLyDo == "")
            {
                e.Valid = false;
                view.SetColumnError(colLyDo, "Tên lý do không được trống");
            }

            //set duyet gia tri trong vong lap for 0-n
            for (int i = 0; i < gvLyDoXoa.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow drLyDo = gvLyDoXoa.GetDataRow(i);
                    string test = drLyDo["TenLyDoXoaCT"].ToString().ToUpper().Trim();
                    //so sanh gia tri can duyet voi gia tri trong vong lap---
                    if (sTenLyDo == test)
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Tên lý do không được trùng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(colLyDo, "Tên lý do không được trùng");
                        return;
                    }
                }
            }
            btnSave.Enabled = true;
        }

        private void gvLyDoXoa_FocusedColumnChanged(object sender, FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }

        private void gvLyDoXoa_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }

        private void gvLyDoXoa_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvLyDoXoa;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }


    }
}